﻿/* 

Custom Toolbar control for PDFWebViewer.NET v. 1.0
--------------------------------------------------

Please check readme.txt for usage details

License
--------------------------------------------------

Copyright (c) 2010 TallComponents BV

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

*/


function PdfToolbar(element, viewer)
{
    this._element = element;
    this._viewer = viewer;
};

PdfToolbar.prototype = {
    init: function (id) {
        // hover effect
        $(".fg-button", this._element).hover(
            function () {
                $(this).addClass("ui-state-hover");
            },
			function () {
			    $(this).removeClass("ui-state-hover");
			}
		);
        // click handler
        $('.fg-button', this._element).click(Function.createDelegate(this, this._onButtonClick));

        this._onViewUpdated(); // get initial state

        // hook up to viewer events
        this._viewer.add_propertyChanged(Function.createDelegate(this, this._onViewPropChanged));
        this._viewer.add_updating(Function.createDelegate(this, this._onViewUpdating));
        this._viewer.add_updated(Function.createDelegate(this, this._onViewUpdated));
    },

    _onViewPropChanged: function (sender, args) {
        // handle external changes to viewer settings
        if (args.get_propertyName() === "cursorMode") {
            // cursorMode was changed; update the toolbar accordingly
            this._updateToolbar();
        }
    },

    _onViewUpdating: function (sender, args) {
        this._showBusySpinner(this._viewer.get_isBusy());
    },

    _onViewUpdated: function (sender, args) {
        this._updateToolbar();
    },

    _updateToolbar: function () {
        this._showBusySpinner(this._viewer.get_isBusy());

        // display current page and page count
        var index = this._viewer.get_pageIndex() + 1;
        var count = this._viewer.get_pageCount();
        $('#navigate-pages', this.element).text((count > 0) ? index + " / " + count : "");

        // manage navigation buttons
        this._setButtonState('navigate-previous', (count > 0 && index > 1), false);
        this._setButtonState('navigate-next', (count > 0 && index < count), false);

        // manage cursor mode buttons
        var mode = this._viewer.get_cursorMode();
        this._setButtonState('cursormode-zoomin', (count > 0), mode == 'zoomIn');
        this._setButtonState('cursormode-zoomout', (count > 0), mode == 'zoomOut');
        this._setButtonState('cursormode-pan', (count > 0), mode == 'pan');

        // display zoom
        var zoom = this._viewer.get_zoom();
        $('#zoom', this.element).text(isNaN(zoom) ? zoom : Math.round(zoom * 100) + '%');

        // manage preset zoom buttons
        this._setButtonState('zoom-width', (count > 0), zoom == 'width');
        this._setButtonState('zoom-page', (count > 0), zoom == 'page');
    },

    _setButtonState: function (buttonId, enabled, active) {
        // set css classes to make sure the button show the correct state
        $('#' + buttonId).toggleClass('ui-state-default', enabled && !active);
        $('#' + buttonId).toggleClass('ui-state-disabled', !enabled);
        $('#' + buttonId).toggleClass('ui-state-active', enabled && active);
    },

    _showBusySpinner: function (visible) {
        // show/hide busy indicator
        $('.busy', this._element).css('visibility', visible ? 'visible' : 'hidden');
    },

    _onButtonClick: function (e) {
        e.preventDefault();

        // get the command from the id of the containing element
        var target = e.target;
        while (target.id.length === 0) {
            if (!target.parentNode) {
                return;
            }
            target = target.parentNode;
        }
        var command = target.id;

        // do nothing for disabled buttons
        if (Sys.UI.DomElement.containsCssClass(target, 'ui-state-disabled')) {
           return;
        }

        // perform action associated with a specific button
        switch (command) {
            case 'navigate-previous':
                var index1 = this._viewer.get_pageIndex();
                if (index1 > 0) { this._viewer.set_pageIndex(index1 - 1); }
                break;
            case 'navigate-next':
                var index2 = this._viewer.get_pageIndex();
                var max = this._viewer.get_pageCount();
                if (index2 < max - 1) { this._viewer.set_pageIndex(index2 + 1); }
                break;
            case 'cursormode-pan':
                this._viewer.set_cursorMode('pan');
                break;
            case 'cursormode-zoomin':
                this._viewer.set_cursorMode('zoomIn');
                break;
            case 'cursormode-zoomout':
                this._viewer.set_cursorMode('zoomOut');
                break;
            case 'zoom-1on1':
                this._viewer.set_zoom(1);
                break;
            case 'zoom-page':
                this._viewer.set_zoom('page');
                break;
            case 'zoom-width':
                this._viewer.set_zoom('width');
                break;
            default:
                break;

        }
    }
};

